const sqlite3 = require('sqlite3').verbose();
const moment = require('moment');
const minuteJ1=(id1)=>{
    let d2=moment(id1,"YYYY-MM-DD HH:mm")
    d2.add(1,"minutes")
    return d2.format("YYYY-MM-DD HH:mm");//"%04d-%02d-%02d %02d:%02d" % (d2.year,d2.month,d2.day,d2.hour,d2.minute)
}
const deleteRow=(conn,row)=>{
    let id1=row.SampleId
    conn.run(`delete from  result where sampleid='${id1}'`)
    conn.close()
}
function delOne2(conn,id2){
    let id1=minuteJ1(id2)
    console.log(id2);
    console.log(id1);
    let cmd=`select * from result where sampleid>='${id2}' and sampleid<'${id1}'` ;
    console.log(cmd);
    conn.each(cmd,(err, row)=>{
        if(err!==null){
            console.log(err);
        }else{
            deleteRow(conn,row)
        }
        return;
    });
}
function dayend(s){//'2016-12-23 14:59'    
    s=s.split(" ")[0]
    s=s+" 23:59"
    return s
}
function treatRow(conn,row,wtb){
    // console.log(row);
    let id1=row.SampleId
    let oldwt=row.SampleWeight
    let wt=oldwt*wtb
    let old01=row.Content01
    let old02=row.Content02
    let old03=row.Content03
    // console.log(oldwt,old01,old02,old03)
    let new01=old01/wtb
    let new02=old02/wtb
    let new03=old03/wtb
    conn.run(`update result set sampleweight=${wt},Content01=${new01},Content02=${new02},Content03=${new03} where sampleid='${id1}'`
        ,(err,row)=>{
            if(err!==null){
                console.log(err);
            }else{
                console.log("ok");
            }
            return;

        })
    conn.close()
}
function changeOne(conn,id2,wtb){
    let id1=minuteJ1(id2)
    console.log(id2);
    console.log(id1);
    let cmd=`select * from result where sampleid>='${id2}' and sampleid<'${id1}'` ;
    console.log(cmd);
    conn.each(cmd,(err, row)=>{
        if(err!==null){
            console.log(err);
        }else{
            treatRow(conn,row,wtb)
        }
        return;
    });
}
function main(){
    let constr=`../data.db`
    let conn = new sqlite3.Database(constr);
    // changeOne(conn,'2021-05-07 13:20',1/1.005)
    delOne2(conn,'2021-05-07 13:31')
}
main()

